package com.example.ribbon.controller;

import com.example.ribbon.service.ConsumerService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @auth leihtg
 * @date 2018/11/27 14:34
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    //方式一
    @Autowired
    RestTemplate restTemplate;

    //方式二
    @Autowired
    private ConsumerService service;

    @Value("${service.provider.name}")
    private String host;

    @RequestMapping("queryInfo")
    @HystrixCommand(fallbackMethod = "helloFallback")
    public String queryInfo() {
        String url = "http://" + host + "/provider/queryInfo";
        return restTemplate.getForEntity(url, String.class).getBody();
    }

    public String helloFallback() {
        return "访问eureka-client失败";
    }

    @RequestMapping("wellcome")
    public String welto(String name) {
        return service.welcome(name);
    }

    @RequestMapping("getKey")
    public String dc(String channelName) {
        return "hello key";
    }
}
